﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ShootSearch.Util;
using ShootSearch.Util.Serializers;
using ShootSearch.Runner.Controls;
using ShootSearch.Runner.Configurations;
using Quartz;
using ShootSearch.Core;
using System.Diagnostics;
using System.IO;

namespace ShootSearch.Runner
{
    public partial class FormNewTask : Form
    {
        public const string JOB_NAME = "Job";
        
        protected NewTaskConfig config = null;
        //protected int jobIndex = 1;
        protected TaskConfig taskConfig = new TaskConfig();
        protected FormConfiguration configForm = new FormConfiguration();
        protected string taskPath = RunnerHelper.TaskPath;

        public FormNewTask()
        {
            InitializeComponent();
            config = NewTaskConfig.Load();
        }

        private void NewTask_Load(object sender, EventArgs e)
        {
            listViewJobs.Items.Clear();
            listViewJobs.Groups.Clear();
            foreach (string item in config.Groups)
            {
                listViewJobs.Groups.Add(new ListViewGroup(item));
            }

            foreach (JobEntity jobEntity in config.Jobs)
            {
                ListViewItem item = new ListViewItem(new string[] 
                { 
                    jobEntity.Assembly.TypeName, jobEntity.Assembly.Assembly 
                });
                item.Tag = jobEntity.Assembly;
                item.ImageIndex = jobEntity.Group;
                item.Group = listViewJobs.Groups[ jobEntity.Group];
                listViewJobs.Items.Add(item);
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (listViewJobs.SelectedItems.Count == 0) return;

            AssemblyConfig assembly = listViewJobs.SelectedItems[0].Tag as AssemblyConfig;

            if (assembly != null)
            {
                try
                {
                    ISpiderMan job = assembly.GetObject() as ISpiderMan;
                    if (job != null)
                    {
                        SpiderManConfig detail = job.Config;
                        JobConfig jobConfig = new JobConfig();
                        jobConfig.Name = job.GetType().FullName;
                        jobConfig.Type = new AssemblyConfig(job.GetType());
                        jobConfig.Config = detail;
                        //jobConfig.Type = new AssemblyConfig(job.GetType());
                       
                        ListViewItem item = listViewJobs.SelectedItems[0].Clone() as ListViewItem;
                        item.Tag = jobConfig;
                        listViewTask.Items.Add(item);

                        //jobIndex++;
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);    
                }
            }
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            if(listViewTask.SelectedItems.Count == 0) return;
            listViewTask.Items.RemoveAt(listViewTask.SelectedItems[0].Index);
          
        }

        public TaskConfig GetTask()
        {
            return taskConfig;
        }

        public string GetPath()
        {
            return textBoxPath.Text;
        }

        public void ShowTaskNamePath()
        {
            if (textBoxName.Text.Length > 0)
            {
                string name = Util.IO.FormatFilename(textBoxName.Text);
                textBoxPath.Text = IO.FormatDirName(taskPath + "\\" + name);
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (listViewTask.SelectedItems.Count == 0) return;
            //JobDetail detail = listViewTask.SelectedItems[0].Tag as JobDetail;
            //object jobConfig = detail.JobDataMap[Task.JOB_CONFIG];
            AssemblyWithConfig jobConfig = listViewTask.SelectedItems[0].Tag as AssemblyWithConfig;
            configForm.Config = jobConfig.Config;

            if (configForm.ShowDialog() == DialogResult.OK)
            {
                jobConfig.Config = configForm.Config as SpiderManConfig;
                listViewTask.SelectedItems[0].Tag = jobConfig;
            }

            
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if(textBoxName.Text.Length ==0 || File.Exists(textBoxPath.Text))
            {
                MessageBox.Show("Name empty or the task has been exist!");
                return;
            }
            
            if (listViewTask.Items.Count == 0)
            {
                this.DialogResult = DialogResult.Cancel;
            }
            else
            {
                taskConfig.TaskName = textBoxName.Text;
                
                foreach (ListViewItem item in listViewTask.Items)
                {
                    JobConfig jobConfig = item.Tag as JobConfig;
                    taskConfig.Jobs.Add(jobConfig);                    
                }
                taskConfig.ContextPath = IO.FormatDirName(textBoxPath.Text) ;

                this.DialogResult = DialogResult.OK;

            }

            
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            ShowTaskNamePath();
        }

        private void buttonChange_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (!string.IsNullOrEmpty(textBoxPath.Text))
            {
                fbd.SelectedPath = textBoxPath.Text;
            }
            else
            {
                fbd.SelectedPath = taskPath;
            }
            //sfd.Filter = "Xml File|*.xml";
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                taskPath = IO.FormatDirName(fbd.SelectedPath);
               
                    ShowTaskNamePath();
              
            }
        }

        private void listViewJobs_DoubleClick(object sender, EventArgs e)
        {
            buttonAdd_Click(sender, e);
        }

        private void listViewTask_DoubleClick(object sender, EventArgs e)
        {
            buttonEdit_Click(sender, e);
        }

        private void listViewTask_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            JobConfig jobConfig = listViewTask.Items[e.Item].Tag as JobConfig;
            jobConfig.Name = e.Label;
        }
    }
}
